<template>
  <div class="con">
    <div class="login">
      <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="用户名" prop="username">
          <el-input type="text" v-model="ruleForm.username" autocomplete="off" size="small" id="username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="pwd">
          <el-input type="pwd" v-model="ruleForm.pwd" autocomplete="off" size="small" id="pwd"></el-input>
        </el-form-item>
        <el-form-item label="验证码" prop="code">
          <el-input v-model="ruleForm.code" size="small"></el-input>
        </el-form-item>
        <el-form-item>
          <span v-html="captcha"></span>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>

export default {
  data() {
    var checkAge = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('验证码不能为空'));
      }
      callback()
    };
    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('用户名不能为空'));
      } else {
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('密码不能为空'));
      } else {
        callback();
      }
    };
    return {
      ruleForm: {
        username: '',
        pwd: '',
        code: ''
      },
      rules: {
        username: [
          { validator: validatePass, trigger: 'blur' }
        ],
        pwd: [
          { validator: validatePass2, trigger: 'blur' }
        ],
        code: [
          { validator: checkAge, trigger: 'blur' }
        ]
      },
      captcha: ""
    };
  },
  methods: {
    submitForm(formName) {
      let userName = document.querySelector("#username");
      let pwd = document.querySelector("#pwd");
      let username = userName.value;
      let password = pwd.value;
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          let result = await this.$axios.post("http://localhost:3000/login", this.ruleForm)
          if (result.data.code == 200) {
            this.$message.success({
              message: '登录成功'
            });
            fetch("http://localhost:3000/token", {
              method: "POST",
              body: JSON.stringify({
                username: username,
                password: password
              })
            }).then(res => res.json())
              .then(res => {
                if (res.token) {
                  localStorage["token"] = res.token
                }
              })
            this.$router.push("/about")
          }
          else {
            this.$message.error({
              message: '登录失败'
            })
          }
        }

        else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    async getCaptcha() {
      let res = await this.$axios.get("http://localhost:3000/captcha")
      this.captcha = res.data;
    }

  },
  mounted() {
    this.getCaptcha();
  }
}
</script>

<style scoped>
.login {
  width: 20%;
  padding: 150px 60px;
  position: absolute;
  top: 0;
  bottom: 0;
  right: 0;
  background: white;
}

.con {
  background: url("../assets/login.jpg");
  width: 200px;
  height: 200px;
  background-repeat: no-repeat;
  background-size: cover;
}
</style>